package com.yc.ui.dialog;

import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Composite;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

import org.eclipse.swt.SWT;
import org.eclipse.wb.swt.SWTResourceManager;

import com.yc.Idao.GoodsIDao;
import com.yc.dao.GoodsDao;
import com.yc.utils.SWTUtil;
import com.yc.utils.StringUtil;

import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;

public class UpdateAndAddGoodsDialog extends Dialog {

	protected Object result;
	protected Shell shell;
	private Text text_price;
	private Text text_name;
	private String filePath = ""; //选中的图片路径
	private Composite com_image;
	private byte[] bt;//图片的字节数组
	private Button btn_update;
	private Button btn_add;
	GoodsIDao dao = new GoodsDao();

	/**
	 * Create the dialog.
	 * @param parent
	 * @param style
	 */
	public UpdateAndAddGoodsDialog(Shell parent, int style) {
		super(parent, style);
		setText("SWT Dialog");
	}

	/**
	 * Open the dialog.
	 * @return the result
	 */
	public Object open() {
		createContents();
		shell.open();
		shell.layout();
		btn_update.setEnabled(false);
		Display display = getParent().getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		return result;
	}
	
	public Object open(Table table) {
		createContents();
		shell.open();
		shell.layout();
		showInfo(table);
		btn_add.setEnabled(false);
		Display display = getParent().getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		return result;
	}

	/**
	 * Create contents of the dialog.
	 */
	private void createContents() {
		shell = new Shell(getParent(), getStyle());
		shell.setSize(606, 350);
		shell.setText(getText());
		SWTUtil.centerWindow(shell);
		
		com_image = new Composite(shell, SWT.NONE);
		com_image.setBackground(SWTResourceManager.getColor(SWT.COLOR_WIDGET_HIGHLIGHT_SHADOW));
		com_image.setBounds(412, 54, 124, 142);
		
		Label lblNewLabel_5 = new Label(shell, SWT.NONE);
		lblNewLabel_5.setText("商品图片");
		lblNewLabel_5.setBounds(330, 57, 76, 20);
		
		btn_add = new Button(shell, SWT.NONE);
		btn_add.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				String name = text_name.getText().toString().trim();
				String price = text_price.getText().toString().trim();
				byte[] image = getImageByte(filePath);
				if(StringUtil.checkNull(name, price)) {
					return;
				}
				int n = dao.addGoods(name, price, image);
				if(n > 0) {
					SWTUtil.showMessage(shell, "添加成功！！！");
					shell.dispose();
				}else {
					SWTUtil.showMessage(shell, "添加失败！！！");
				}
			}
		});
		btn_add.setBounds(79, 248, 98, 30);
		btn_add.setText("添加");
		
		btn_update = new Button(shell, SWT.NONE);
		btn_update.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				String name = text_name.getText().toString().trim();
				String price = text_price.getText().toString().trim();
				if(StringUtil.checkNull(name, price)) {
					return;
				}
				int n = dao.updateGoods(name, price);
				if(n > 0) {
					SWTUtil.showMessage(shell, "修改成功！！！");
					shell.dispose();
				}else {
					SWTUtil.showMessage(shell, "修改失败！！！");
					return ;
				}
			}
		});
		btn_update.setBounds(253, 248, 98, 30);
		btn_update.setText("修改");
		
		Button btnNewButton_2 = new Button(shell, SWT.NONE);
		btnNewButton_2.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				shell.dispose();
			}
		});
		btnNewButton_2.setBounds(424, 248, 98, 30);
		btnNewButton_2.setText("取消");
		
		text_price = new Text(shell, SWT.BORDER);
		text_price.setBounds(161, 138, 81, 26);
		
		Label lblNewLabel_4 = new Label(shell, SWT.NONE);
		lblNewLabel_4.setText("商品价格");
		lblNewLabel_4.setBounds(69, 141, 76, 20);
		
		Label lblNewLabel_2 = new Label(shell, SWT.NONE);
		lblNewLabel_2.setText("商品名称");
		lblNewLabel_2.setBounds(69, 54, 76, 20);
		
		text_name = new Text(shell, SWT.BORDER);
		text_name.setBounds(158, 54, 118, 26);
		
		Label lblNewLabel_6_1 = new Label(shell, SWT.NONE);
		lblNewLabel_6_1.setText("元");
		lblNewLabel_6_1.setBounds(253, 141, 24, 20);
		
		com_image.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDoubleClick(MouseEvent e) {
				//上传文件的框
				FileDialog fd = new FileDialog(shell);
				//默认路径
				fd.setFilterPath("SystemRoot");
				//限定上传图片格式
				fd.setFilterExtensions(new String[] {"*.*", "*.jdp", "*.png", "*.gif"});
				String select = fd.open();
				if(StringUtil.checkNull(select)) {
					SWTUtil.showMessage(shell, "请选择你要上传的图片！！！");
					return;
				}
				//将选中的图片的绝对路径储存到全局变量
				filePath = select;
				try {
					File file = new File(select);
					//将图片设置到com_image
					//Image image = new Image(Display.getCurrent(), new FileInputStream(file));
					//com_image.setBackgroundImage(image);
					//调用封装后的工具类设置 自适应封面背景
					com_image.setBackgroundImage(SWTUtil.imageSize(com_image, new FileInputStream(file)));
				} catch (FileNotFoundException e1) {
					e1.printStackTrace();
				}
			}
		});
	}
	
	protected byte[] getImageByte(String filePath) {
		byte [] bimage = null;
		//存入图片
		if(!StringUtil.checkNull(filePath)) {
			File file = new File(filePath);
			try(InputStream in = new FileInputStream(file)) {
				byte[] bt = new byte[(int) file.length()];
				while(in.read(bt) != -1) {
					//存入字节数组
					bimage = bt;
				}
			}catch(Exception e) {
				e.printStackTrace();
			}
		}
		return bimage;
	}
	
	private void showInfo(Table table) {
		//获取表格中被选中行
		TableItem item = table.getItem(table.getSelectionIndex());
		text_name.setText(item.getText(1));
		text_price.setText(item.getText(2));
		//获取封面
		Image image = item.getImage(3);
		if(image != null) {
			com_image.setBackgroundImage(image);
			SWTUtil.imagetSize(com_image);
		}else {
			com_image.setBackgroundImage(null);
		}
	}
}
